package javolution.context;

import javolution.lang.Configurable;
import javolution.lang.ValueType;
import javolution.util.FastMap;
import javolution.util.FastTable;

/* loaded from: classes2.dex */
public abstract class StackContext extends AllocatorContext {
    public static final Configurable<Class<? extends StackContext>> DEFAULT = new Configurable<>(Default.CLASS);
    public static final Configurable<Boolean> DISABLED = new Configurable<>(new Boolean(false));
    private boolean _isDisabled = false;

    /* loaded from: classes2.dex */
    private static final class Default extends StackContext {
        private static final Class CLASS = new Default().getClass();
        private final ThreadLocal _activeAllocators;
        private final ThreadLocal _factoryToAllocator;
        private final FastTable _nonOwnerUsedAllocators;
        private final FastTable _ownerUsedAllocators;

        private Default() {
            this._factoryToAllocator = new ThreadLocal() { // from class: javolution.context.StackContext.Default.1
                @Override // java.lang.ThreadLocal
                protected Object initialValue() {
                    return new FastMap();
                }
            };
            this._activeAllocators = new ThreadLocal() { // from class: javolution.context.StackContext.Default.2
                @Override // java.lang.ThreadLocal
                protected Object initialValue() {
                    return new FastTable();
                }
            };
            this._ownerUsedAllocators = new FastTable();
            this._nonOwnerUsedAllocators = new FastTable();
        }

        /* JADX INFO: Access modifiers changed from: protected */
        @Override // javolution.context.AllocatorContext
        public void deactivate() {
            FastTable fastTable = (FastTable) this._activeAllocators.get();
            int size = fastTable.size();
            for (int i = 0; i < size; i++) {
                ((Allocator) fastTable.get(i)).user = null;
            }
            fastTable.clear();
        }

        @Override // javolution.context.Context
        protected void enterAction() {
            getOuter().getAllocatorContext().deactivate();
        }

        @Override // javolution.context.Context
        protected void exitAction() {
            deactivate();
            for (int i = 0; i < this._ownerUsedAllocators.size(); i++) {
                ((StackAllocator) this._ownerUsedAllocators.get(i)).reset();
            }
            this._ownerUsedAllocators.clear();
            for (int i2 = 0; i2 < this._nonOwnerUsedAllocators.size(); i2++) {
                ((StackAllocator) this._nonOwnerUsedAllocators.get(i2)).reset();
            }
            this._nonOwnerUsedAllocators.clear();
        }

        /* JADX INFO: Access modifiers changed from: protected */
        @Override // javolution.context.AllocatorContext
        public Allocator getAllocator(ObjectFactory objectFactory) {
            if (isDisabled()) {
                return getOuter().getAllocatorContext().getAllocator(objectFactory);
            }
            FastMap fastMap = (FastMap) this._factoryToAllocator.get();
            StackAllocator stackAllocator = (StackAllocator) fastMap.get(objectFactory);
            if (stackAllocator == null) {
                stackAllocator = new StackAllocator(objectFactory);
                fastMap.put(objectFactory, stackAllocator);
            }
            if (stackAllocator.user == null) {
                stackAllocator.user = Thread.currentThread();
                ((FastTable) this._activeAllocators.get()).add(stackAllocator);
            }
            if (!stackAllocator._inUse) {
                stackAllocator._inUse = true;
                if (Thread.currentThread() == getOwner()) {
                    this._ownerUsedAllocators.add(stackAllocator);
                } else {
                    synchronized (this._nonOwnerUsedAllocators) {
                        this._nonOwnerUsedAllocators.add(stackAllocator);
                    }
                }
            }
            return stackAllocator;
        }
    }

    /* loaded from: classes2.dex */
    private static final class StackAllocator extends Allocator {
        private final ObjectFactory _factory;
        private boolean _inUse;
        private int _queueLimit;

        public StackAllocator(ObjectFactory objectFactory) {
            this._factory = objectFactory;
            this.keepInQueue = true;
        }

        /* JADX WARN: Multi-variable type inference failed */
        @Override // javolution.context.Allocator
        protected Object allocate() {
            if (this._queueLimit >= this.queue.length) {
                resize();
            }
            Object create = this._factory.create();
            T[] tArr = this.queue;
            int i = this._queueLimit;
            this._queueLimit = i + 1;
            tArr[i] = create;
            return create;
        }

        /* JADX INFO: Access modifiers changed from: protected */
        /* JADX WARN: Multi-variable type inference failed */
        @Override // javolution.context.Allocator
        public void recycle(Object obj) {
            if (this._factory.doCleanup()) {
                this._factory.cleanup(obj);
            }
            for (int i = this.queueSize; i < this._queueLimit; i++) {
                if (this.queue[i] == obj) {
                    this.queue[i] = this.queue[this.queueSize];
                    T[] tArr = this.queue;
                    int i2 = this.queueSize;
                    this.queueSize = i2 + 1;
                    tArr[i2] = obj;
                    return;
                }
            }
            throw new UnsupportedOperationException("Cannot recycle to the stack an object which has not been allocated from the stack");
        }

        protected void reset() {
            this._inUse = false;
            while (this._factory.doCleanup() && this.queueSize != this._queueLimit) {
                Object[] objArr = this.queue;
                int i = this.queueSize;
                this.queueSize = i + 1;
                this._factory.cleanup(objArr[i]);
            }
            this.queueSize = this._queueLimit;
        }

        public String toString() {
            return "Stack allocator for " + this._factory.getClass();
        }
    }

    static {
        ObjectFactory.setInstance(new ObjectFactory() { // from class: javolution.context.StackContext.1
            /* JADX INFO: Access modifiers changed from: protected */
            @Override // javolution.context.ObjectFactory
            public Object create() {
                return new Default();
            }
        }, Default.CLASS);
    }

    public static StackContext enter() {
        StackContext stackContext = (StackContext) Context.enter((Class<Context>) DEFAULT.get());
        stackContext._isDisabled = DISABLED.get().booleanValue();
        return stackContext;
    }

    public static StackContext exit() {
        return (StackContext) Context.exit();
    }

    public static <T extends ValueType> T outerCopy(T t) {
        StackContext stackContext = (StackContext) AllocatorContext.getCurrent();
        boolean isDisabled = stackContext.isDisabled();
        stackContext.setDisabled(true);
        Object copy = t.copy();
        stackContext.setDisabled(isDisabled);
        return (T) copy;
    }

    public static void outerExecute(Runnable runnable) {
        StackContext stackContext = (StackContext) AllocatorContext.getCurrent();
        boolean isDisabled = stackContext.isDisabled();
        stackContext.setDisabled(true);
        runnable.run();
        stackContext.setDisabled(isDisabled);
    }

    public final boolean isDisabled() {
        return this._isDisabled;
    }

    public final void setDisabled(boolean z) {
        if (z == this._isDisabled) {
            return;
        }
        if (z) {
            deactivate();
        } else {
            getOuter().getAllocatorContext().deactivate();
        }
        this._isDisabled = z;
    }
}
